#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

// ╔═════════════════════╗
// ║   下班标识;　　       ║
// ╚═════════════════════╝
volatile int off_duty_flag = 0;

// ╔═════════════════════╗
// ║   打印当前状态;       ║
// ╚═════════════════════╝
void printf_state(char *state)
{
    char len[20] = {0};

    time_t timep;
    time(&timep);

    struct tm *p;
    p = gmtime(&timep);

    snprintf(len, 20, "%d-%d-%d %d:%d:%d", 1900 + p->tm_year, 1 + p->tm_mon, p->tm_mday, 8 + p->tm_hour, p->tm_min, p->tm_sec);

    printf("\n%s :  %lu ---> %s\n", len, pthread_self(), state);
}

// ╔══════════════════════════╗
// ║   是否有订单标识;　　       ║
// ╚══════════════════════════╝
char *has_order = NULL;
void (*handler_order)(char *) = NULL;

// ╔═════════════════════╗
// ║   点外卖的方法;       ║
// ╚═════════════════════╝
void place_order(char *order, void (*handler)(char *))
{
    printf_state("shuouyang: place_order...\n");
    has_order = order;
    handler_order = handler;
}

// ╔════════════════╗
// ║   订单到达了；   ║
// ╚════════════════╝
void arrived_order()
{
    (*handler_order)(has_order);
    has_order = NULL;
    handler_order = NULL;
}

// ╔═════════════════════╗
// ║   外卖骑手;　　       ║
// ╚═════════════════════╝
void *takeaway_rider()
{
    int running = 0; // 是否是在派送中
    while (off_duty_flag == 0)
    {
        if (running == 0) // 没有派送
        {
            if (has_order != NULL) // 发现有订单
            {
                printf_state("rider: 接到订单了...\n");
                char *user, *food;
                user = strtok(has_order, "|");
                food = strtok(NULL, "|");
                has_order = food;
                char tem[100] = {0};
                sprintf(tem, "rider: 接收到%s的%s订单; \n", user, food);
                printf_state(tem);
                running = 1; //派送中
            }
            else
            {
                printf_state("rider: 等待接单中...\n");
            }
        }
        else
        {                                   //派送中
            int sec = time((time_t *)NULL); //返回秒级时间戳
            if (sec % 10 == 0)              // 表示到点了
            {
                printf_state("rider :订单已经送达.\n");
                running = 0; //派送成功
                arrived_order();
            }
            else
            {
                printf_state("rider :订单还在路上...\n");
            }
        }
        sleep(1);
    }
}

void eat_food(char *food)
{
    char tem[100] = {0};
    sprintf(tem, "shuouyang: 吃到了 -> %s\n", food);
    printf_state(tem);
    off_duty_flag = 1;
}

// ╔════════════════╗
// ║   打工人;       ║
// ╚════════════════╝
void shuouyang()
{
    int is_eat = 0; //是否吃过饭了
    printf_state("shuouyang: I am shuouyang.\n");
    while (off_duty_flag == 0)
    {
        int sec = time((time_t *)NULL); //返回秒级时间戳
        if (sec % 10 == 0)              // 表示到点了
        {
            printf_state("shuouyang: 到点了该点外卖了...\n");
            if (is_eat == 0) // 没有吃饭，该点外卖了
            {
                char order[30] = "shuouyang|肯德基";
                place_order(order, eat_food);
            }
        }
        else
        {
            printf_state("shuouyang: 我在工作...\n");
        }
        sleep(1);
    }
    printf_state("shuouyang: 我下班啦.\n");
}

int main()
{
    pthread_t rider;
    pthread_create(&rider, NULL, takeaway_rider, NULL);
    shuouyang();
    pthread_join(rider, NULL);
    printf_state("今天结束了.\n");
}
